﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


    namespace LY.Manage.Common
    {
        public class SerializeHelper
        {
            /// <summary>
            /// 对数据进行序列化
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public static string SerializeToString(object value)
            {
                return JsonConvert.SerializeObject(value);
                //JsonSerializerSettings jsSettings = new JsonSerializerSettings();
                //jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //return JsonConvert.SerializeObject(value,jsSettings);
            }
            public static string SerializeLoopToString(object value)
            {
                //return JsonConvert.SerializeObject(value);
                JsonSerializerSettings jsSettings = new JsonSerializerSettings();
                jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                return JsonConvert.SerializeObject(value, jsSettings);
            }

            /// <summary>
            /// 对数据进行序列化,解决循环引用  JsonSerializerSettings jsSettings = new JsonSerializerSettings()
            /// </summary>
            /// <param name="value"></param>
            /// <param name="jsSettings"> jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; </param>
            /// <returns></returns>
            public static string SerializeToString(object value, JsonSerializerSettings jsSettings)
            {
                //下面两句可以解决循环引用问题
                //JsonSerializerSettings jsSettings = new JsonSerializerSettings();
                //jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                return JsonConvert.SerializeObject(value, jsSettings);
            }
            /// <summary>
            /// 反序列化操作
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="str"></param>
            /// <returns></returns>
            public static T DeserializeToObject<T>(string str)
            {
                return JsonConvert.DeserializeObject<T>(str);
            }
        }
    }

